package GUI;

import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@SuppressWarnings("serial")
/**
 * Build an activity report according to entered period of time
 * @author Benny
 *
 */
public class ActivityReportGUI extends JPanel {
	
	public MainFrame mf;
	public JLabel lblActive;
	public JLabel lblFrozen;
	public JLabel lblLock;
	public JLabel lblBorrow;
	public JLabel lblDelay;
    private JTextField textFrom;
    private JTextField textTo;
	

	/**
	 * initialize all the text fields
	 */
	public void init(){
		lblActive.setText("");
		lblFrozen.setText("");
		lblLock.setText("");
		lblBorrow.setText("");
		lblDelay.setText("");
		textFrom.setText("YYYY-MM-DD");
		textTo.setText("YYYY-MM-DD");
	}
	/**
	 * check for string validity (needs to contain date in format: YYYY-MM-DD)
	 * @param date given specific string and checks for its validity
	 * @return validity status
	 */
	public int checkDate(String date){
        String expression = "^\\(?(\\d{4})\\)?[- ]?(\\d{2})[- ]?(\\d{2})$";
        CharSequence inputStr = date;
        Pattern pattern = Pattern.compile(expression);
        Matcher matcher = pattern.matcher(inputStr);
        boolean matchFound = matcher.matches();

		if(matchFound) return 1;
		else return 0;
    }
	/**
	 * Create the panel.
	 * @param M main frame of the program
	 */
	public ActivityReportGUI(MainFrame M) {
		setLayout(null);
		mf=M;
		JLabel lblActivityReport = new JLabel("Activity Report");
		lblActivityReport.setFont(new Font("Segoe UI", Font.BOLD, 16));
		lblActivityReport.setBounds(231, 12, 191, 22);
		add(lblActivityReport);
		
		JLabel lblNumberOfActive = new JLabel("Number of active Users:");
		lblNumberOfActive.setBounds(75, 141, 153, 15);
		add(lblNumberOfActive);
		
		JLabel lblNumberOfFrozen = new JLabel("Number of frozen Users:");
		lblNumberOfFrozen.setBounds(75, 186, 153, 15);
		add(lblNumberOfFrozen);
		
		JLabel lblNumberOfLocked = new JLabel("Number of locked Users:");
		lblNumberOfLocked.setBounds(75, 230, 153, 15);
		add(lblNumberOfLocked);
		
		JLabel lblNumberOfActive_1 = new JLabel("Number of active borrows:");
		lblNumberOfActive_1.setBounds(75, 271, 153, 15);
		add(lblNumberOfActive_1);
		
		JLabel lblNumberOfDelayed = new JLabel("Number of delayed returns:");
		lblNumberOfDelayed.setBounds(75, 311, 163, 15);
		add(lblNumberOfDelayed);
		
		lblActive = new JLabel("");
		lblActive.setBounds(348, 141, 163, 15);
		add(lblActive);
		
		lblFrozen = new JLabel("");
		lblFrozen.setBounds(348, 186, 163, 15);
		add(lblFrozen);
		
		lblLock = new JLabel("");
		lblLock.setBounds(348, 230, 163, 15);
		add(lblLock);
		
		lblBorrow = new JLabel("");
		lblBorrow.setBounds(348, 271, 163, 15);
		add(lblBorrow);
		
		lblDelay = new JLabel("");
		lblDelay.setBounds(348, 311, 163, 15);
		add(lblDelay);
		
		
		
		JButton button = new JButton("View");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				mf.managerMain.reports.setVisible(false);
				mf.managerMain.cardManaging.setVisible(false);
				mf.managerMain.activityGUI.setVisible(true);
				
				mf.librarianMain.bookReturn.setVisible(false);
				mf.librarianMain.bookBorrow.setVisible(false);
				mf.librarianMain.manualExtend.setVisible(false);
				mf.librarianMain.searchBook.setVisible(false);
				mf.librarianMain.viewUser.setVisible(false);
				mf.librarianMain.registerNewUser.setVisible(false);
				if(checkDate(textFrom.getText())==1 && checkDate(textTo.getText())==1)
				{
				ArrayList<Object> send = new ArrayList<>();
				send.add("3");
				send.add("Activity");
				send.add(textFrom.getText());
				send.add(textTo.getText());
				mf.client.accept(send);
				}
				else JOptionPane.showMessageDialog(null, "wrong date!", "Program", JOptionPane.ERROR_MESSAGE);
			}
		});
		button.setBounds(383, 64, 95, 23);
		add(button);
		
		textTo = new JTextField();
		textTo.setText("YYYY-MM-DD");
		textTo.setColumns(10);
		textTo.setBounds(277, 65, 96, 21);
		add(textTo);
		
		JLabel lblTo = new JLabel("To:");
		lblTo.setBounds(250, 68, 33, 15);
		add(lblTo);
		
		textFrom = new JTextField();
		textFrom.setText("YYYY-MM-DD");
		textFrom.setColumns(10);
		textFrom.setBounds(158, 65, 82, 21);
		add(textFrom);
		
		JLabel lblFrom = new JLabel("From:");
		lblFrom.setBounds(114, 68, 51, 15);
		add(lblFrom);
		
		ImageIcon img = new ImageIcon(getClass().getResource(
                "/util/grad2.jpg"));
		JLabel picLabel = new JLabel(img);
		picLabel.setLocation(0, 0);
		picLabel.setSize(683, 575);
		add(picLabel);

	}

}
